/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tst;

public class TST {

    private TSTNode rootNode;
    private TSTNode currentNode;

    public void buildTree() {
        rootNode = new TSTNode();
    }

    public void getInfo() {
        System.out.println(rootNode.getLokid().getEqkid().getValue());
    }

    public void insert(String key, Object value) {
        int charIndex = 0;
        currentNode = rootNode;
        TSTNode newNode;

        while (true) {
            /*if (currentNode.getSplitChar() == ' ') {
                currentNode.setSplitChar(key.charAt(charIndex));
                rootNode = currentNode;
                if (key.length() == 1) {
                    break;
                }
            } else */
            if (key.charAt(charIndex) == currentNode.getSplitChar()) {
                charIndex++;
                if (key.length() == charIndex) {
                    currentNode.setValue(value);
                    break;
                } else if (currentNode.getEqkid() == null) {
                    newNode = new TSTNode();
                    currentNode.setEqkid(newNode);
                    newNode.setSplitChar(key.charAt(charIndex));
                    currentNode = newNode;
                } else {
                    currentNode = currentNode.getEqkid();
                }
            } else if (key.charAt(charIndex) < currentNode.getSplitChar()) {
                if (currentNode.getLokid() == null) {
                    newNode = new TSTNode();
                    currentNode.setLokid(newNode);
                    newNode.setSplitChar(key.charAt(charIndex));
                } else {
                    currentNode = currentNode.getLokid();
                }
            } else if (key.charAt(charIndex) > currentNode.getSplitChar()) {
                if (currentNode.getHikid() == null) {
                    newNode = new TSTNode();
                    currentNode.setHikid(newNode);
                    newNode.setSplitChar(key.charAt(charIndex));
                } else {
                    currentNode = currentNode.getHikid();
                }
            }
        }
    }

    public Object getValue(String key) {
        Object value = null;
        int charIndex = 0;
        currentNode = rootNode;

        while (true) {
            if (key.charAt(charIndex) == currentNode.getSplitChar()) {
                charIndex++;
                if (charIndex == key.length()) {
                    if ((Object) currentNode.getValue() == null) {
                        value = null;
                        break;
                    } else {
                        value = currentNode.getValue();
                        break;
                    }
                } else {
                    if((Object)currentNode.getEqkid()==null){
                        value=null;
                        break;
                    }else{
                        currentNode = currentNode.getEqkid();
                    }
                }
            } else if (key.charAt(charIndex) < currentNode.getSplitChar()) {
                if (currentNode.getLokid() == null) {
                    value = null;
                    break;
                } else {
                    currentNode = currentNode.getLokid();
                }
            } else if (key.charAt(charIndex) > currentNode.getSplitChar()) {
                if (currentNode.getHikid() == null) {
                    value = null;
                    break;
                } else {
                    currentNode = currentNode.getHikid();
                }
            }
        }
        return value;
    }

    public boolean getIsExist(String key) {
        boolean isExist = false;
        int charIndex = 0;
        currentNode = rootNode;

        while (true) {
            if (key.charAt(charIndex) == currentNode.getSplitChar()) {
                charIndex++;
                if (key.length() == charIndex) {
                    if ((Object) currentNode.getValue() == null) {
                        isExist = false;
                        break;
                    } else {
                        isExist = true;
                        break;
                    }
                } else {
                    currentNode = currentNode.getEqkid();
                }
            } else if (key.charAt(charIndex) < currentNode.getSplitChar()) {
                if (currentNode.getLokid() == null) {
                    isExist = false;
                    break;
                } else {
                    currentNode = currentNode.getLokid();
                }
            } else if (key.charAt(charIndex) > currentNode.getSplitChar()) {
                if (currentNode.getHikid() == null) {
                    isExist = false;
                    break;
                } else {
                    currentNode = currentNode.getHikid();
                }
            }
        }
        return isExist;
    }
}
